<template>
  <div class="role-configuration-container">
    <ContentWrap class="role-wrapper">
      <!-- 角色配置头部 -->
      <div class="role-header">
        <div class="header-left">
          <div class="header-icon">
            <Icon icon="ep:user" />
          </div>
          <div class="header-content">
            <h4 class="role-title">角色配置</h4>
            <p class="role-subtitle">管理租户的角色权限配置</p>
          </div>
        </div>
        <el-button
          type="primary"
          v-hasPermi="['system:tenant:create']"
          @click="openForm('create')"
          class="add-btn"
        >
          <Icon icon="ep:plus" class="btn-icon" />
          新增
        </el-button>
      </div>

      <!-- 表格容器 -->
      <div class="table-container">
        <el-table
          v-loading="loading"
          :data="list"
          border
          class="enhanced-table"
          :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
        >
          <el-table-column align="center" label="角色名称" prop="name">
            <template #default="scope">
              <div class="name-cell">
                <Icon icon="ep:user" class="name-icon" />
                <span class="name-text">{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" label="角色标识" prop="code">
            <template #default="scope">
              <div class="code-cell">
                <Icon icon="ep:key" class="code-icon" />
                <span class="code-text">{{ scope.row.code }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" label="备注" prop="remark">
            <template #default="scope">
              <div class="remark-cell">
                <Icon icon="ep:chat-line-round" class="remark-icon" />
                <span class="remark-text">{{ scope.row.remark || '-' }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" label="状态" prop="status" width="100">
            <template #default="scope">
              <div class="status-cell">
                <dict-tag
                  :type="DICT_TYPE.COMMON_STATUS"
                  :value="scope.row.status"
                  class="enhanced-dict-tag"
                />
              </div>
            </template>
          </el-table-column>

          <el-table-column
            :formatter="dateFormatter"
            align="center"
            label="创建时间"
            prop="createTime"
            width="200"
          >
            <template #default="scope">
              <div class="time-cell">
                <Icon icon="ep:clock" class="time-icon" />
                <span>{{ dateFormatter(scope.row, null, scope.row.createTime) }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column :width="300" align="center" label="操作" fixed="right">
            <template #default="scope">
              <div class="action-buttons">
                <el-button
                  v-hasPermi="['system:tenant:update']"
                  link
                  type="primary"
                  @click="openForm('update', scope.row.id)"
                  class="action-btn edit-btn"
                  size="small"
                >
                  <Icon icon="ep:edit" class="btn-icon" />
                  编辑
                </el-button>

                <el-button
                  v-hasPermi="['system:tenant:update']"
                  link
                  type="primary"
                  @click="openAssignMenuForm(scope.row)"
                  class="action-btn menu-btn !ml-0"
                  size="small"
                >
                  <Icon icon="ep:menu" class="btn-icon" />
                  菜单权限
                </el-button>

                <el-button
                  v-hasPermi="['system:tenant:update']"
                  link
                  type="primary"
                  @click="openDataPermissionForm(scope.row)"
                  class="action-btn data-btn !ml-0"
                  size="small"
                >
                  <Icon icon="ep:coin" class="btn-icon" />
                  数据权限
                </el-button>

                <el-button
                  v-hasPermi="['system:tenant:delete']"
                  link
                  type="danger"
                  @click="handleDelete(scope.row.id)"
                  class="action-btn delete-btn !ml-0"
                  size="small"
                >
                  <Icon icon="ep:delete" class="btn-icon" />
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <Pagination
          v-model:limit="queryParams.pageSize"
          v-model:page="queryParams.pageNo"
          :total="total"
          @pagination="getList"
        />
      </div>
    </ContentWrap>

    <!-- 表单弹窗：添加/修改 -->
    <Roleform ref="formRef" @success="getList" />
    <!-- 表单弹窗：菜单权限 -->
    <RoleAssignMenuForm ref="assignMenuFormRef" @success="getList" />
    <!-- 表单弹窗：数据权限 -->
    <RoleDataPermissionForm ref="dataPermissionFormRef" @success="getList" />
  </div>
</template>

<script setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as RoleApi from '@/api/system/role'
import Roleform from './RoleForm.vue'
import RoleAssignMenuForm from './RoleAssignMenuForm.vue'
import RoleDataPermissionForm from './RoleDataPermissionForm.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(false)
const list = ref([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  tenantId: null
})
const props = defineProps({
  configureId: String
})
/*保存*/
const preservationConfig = async () => {
  await TenantApi.updateTenant(formData.value)
  message.success(t('common.updateSuccess'))
}
/** 查询角色列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RoleApi.getRolePageAdmin(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
watch(
  () => props.configureId,
  (value) => {
    if (value) {
      queryParams.tenantId = value
      getList()
    }
  },
  { immediate: true, deep: true }
)
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type, id) => {
  formRef.value.open(type, id, queryParams.tenantId)
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RoleApi.deleteRoleAdmin({ id, tenantId: queryParams.tenantId })
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 菜单权限操作 */
const assignMenuFormRef = ref()
const openAssignMenuForm = async (row) => {
  assignMenuFormRef.value.open(row, queryParams.tenantId)
}
/** 数据权限操作 */
const dataPermissionFormRef = ref()
const openDataPermissionForm = async (row) => {
  dataPermissionFormRef.value.open(row, queryParams.tenantId)
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.role-configuration-container {
  padding: 0;
}

.role-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

/* 角色配置头部 */
.role-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 16px;
  }
}

.header-left {
  display: flex;
  align-items: center;
  flex: 1;

  @media (max-width: 768px) {
    justify-content: center;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 10px;
  }
}

.role-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.role-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.add-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
  color: white;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    background: white;
    color: $primary-color;
    border-color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(255, 255, 255, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 表格容器 */
.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.name-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.name-icon {
  color: $primary-color;
  font-size: 16px;
  flex-shrink: 0;
}

.name-text {
  font-weight: 600;
  color: $text-primary;
}

.code-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.code-icon {
  color: $warning-color;
  font-size: 14px;
  flex-shrink: 0;
}

.code-text {
  font-weight: 500;
  color: $text-regular;
  font-family: 'Courier New', monospace;
  font-size: 13px;
}

.remark-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.remark-icon {
  color: $info-color;
  font-size: 14px;
  flex-shrink: 0;
}

.remark-text {
  color: $text-regular;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-cell {
  display: flex;
  justify-content: center;
  align-items: center;
}

.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
  font-size: 12px;
  color: $text-secondary;
}

.time-icon {
  color: $text-secondary;
  font-size: 14px;
}

.enhanced-dict-tag {
  border-radius: 6px;
  font-weight: 500;
  padding: 4px 8px;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 3px 6px;
  }
}

/* 操作按钮样式 */
.action-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 4px;
  }
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;
  white-space: nowrap;

  &:hover {
    transform: scale(1.05);
  }

  .btn-icon {
    font-size: 12px;
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
    justify-content: center;
  }

  &.edit-btn:hover {
    background: $primary-light;
    color: $primary-color;
  }

  &.menu-btn:hover {
    background: rgba(103, 194, 58, 0.1);
    color: $success-color;
  }

  &.data-btn:hover {
    background: rgba(230, 162, 60, 0.1);
    color: $warning-color;
  }

  &.delete-btn:hover {
    background: rgba(245, 108, 108, 0.1);
    color: $danger-color;
  }
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .role-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .role-title {
    color: #f9fafb;
  }

  .name-text,
  .code-text {
    color: #f9fafb;
  }

  .remark-text {
    color: #d1d5db;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .role-header {
    padding: 12px 16px;
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .pagination-wrapper {
    padding: 12px;
  }

  .remark-text {
    max-width: 120px;
  }

  .code-text {
    font-size: 11px;
  }
}
</style>
